//
//  ViewController.m
//  通讯录
//
//  Created by likexian on 16-4-28.
//  Copyright (c) 2016年 ZF. All rights reserved.
//

#import "ViewController.h"
#import "Phonedata.h"
@interface ViewController () <UITableViewDataSource>
{
    NSArray *data;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title=@"通讯录";
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//bounds 默认边界为零原点
   
    //[tableView setDataSoure:self]
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];

    NSBundle *bundle = [NSBundle  mainBundle];
    
    NSURL *url = [bundle URLForResource:@"00001" withExtension:@"vcf"];//NSURL 请求地址
    
    NSString *str = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    
    NSArray *arr = [str componentsSeparatedByString:@"\n"];
    NSMutableArray *link = [NSMutableArray new];
    Phonedata *item;
    
    for(NSString *str2 in arr)
    {
        if ([str2 hasPrefix:@"BEGIN"]) {
             item = [Phonedata new];
        }//hasPrefix 判断字符串是否以指定字符串开头
        
        else if([str2 hasPrefix:@"END"])
        {
            [link addObject:item];//addObject: 在数组末尾添加对象
        }
        
        else if([str2 hasPrefix:@"FN"])
        {
            NSArray *arr2 = [str2 componentsSeparatedByString:@":"];
            NSString *str = arr2[1];
            item.name = str;
        }
        
        else if([str2 hasPrefix:@"TEL"])
        {
            NSArray *arr3 = [str2 componentsSeparatedByString:@":"];
            NSString *str = arr3[1];
            item.tel = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
            //stringByReplacingOccurrencesOfString: withString: // 替换出现的字符
        }
    }
    data = link;
}

/*-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

}*/


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    return data.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    int row = [indexPath row];
    Phonedata  *item = [data objectAtIndex:row];
    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.tel;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
